<template>
  <div class="ticketexpirydate">
    <div class="ct">
      <Form ref="formValidate"
            :model="formValidate"
            :rules="ruleValidate"
            :label-width="150">
        <FormItem label="有效期(月)："
                  prop="month">
          <Input v-model="formValidate.month"
                 @on-keyup="changemonth"
                 disabled
                 :maxlength="5"
                 @on-blur="blurmonth"
                 style="width:280px;"></Input>
        </FormItem>
        <FormItem>
          <Button type="primary"
                  class="button"
                  style="margin-right:24px;"
                  @click="resubmit"
                  v-if="false">确定</Button>
          <Button class="button btn1"
                  @click="cancel">取消</Button>
        </FormItem>
      </Form>
    </div>
  </div>
</template>
<script>
import { getDicQuery, setDicSave } from '@/api/setting'
import { routeEqual } from '@/libs/util'
export default {
  data () {
    return {
      formValidate: {
        month: '12'
      },
      ruleValidate: {
        month: [
          { required: true, message: '有效期不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    changemonth () {
      this.formValidate.month = this.formValidate.month.replace(/^(0+)|[^\d]+/g, '')
    },
    resubmit () {
      this.$refs.formValidate.validate((val) => {
        if (val) {
          setDicSave({
            'lottery_effective_date': this.formValidate.month,
            'merchantId': this.$store.state.user.merchantId,
            'msType': '0'
          }).then(res => {
            if (res.data.status === 200) {
              this.$Message.success('修改成功')
              this.closeModal()
            } else {
              this.$Message.error(`${res.data.error}`)
            }
          })
        }
      })
    },
    submitForm () {
      getDicQuery({
        'dictKeys': 'lottery_effective_date',
        'merchantId': this.$store.state.user.merchantId,
        'msType': '0'
      }).then((res) => {
        if (res.data.status === 200) {
          this.formValidate.month = String(res.data.data[0].lottery_effective_date)
        } else {
          this.$Message.error(`${res.data.error}`)
        }
      })
    },
    cancel () {
      this.closeModal()
    },
    closeModal () {
      this.$store.commit('closeTag', this.$route)
      this.$store.commit('setTagNavList', this.$store.state.app.tagNavList.filter(item => !routeEqual(this.$route, item)))
      this.$nextTick(() => {
        this.$router.push({
          name: 'global_setting'
        })
      })
    },
    blurmonth () {
      if (/^[a-zA-Z]+$/.test(this.formValidate.month)) {
        this.formValidate.month = ''
      }
    }
  },
  mounted () {
    this.submitForm()
    if (!this.hasPermission('moms:globalsetting:ticketexpirydate')) {
      this.$router.replace({ name: 'error_401' })
    }
  }
}
</script>
<style lang="less" scoped>
.ticketexpirydate {
  .ct {
    width: 450px;
    margin: auto;
    padding-top: 100px;
  }
}
</style>
